<template>
  <div class="app-container" v-if="dialogFormVisible">
    <el-dialog :title="pageTitle" :visible.sync="dialogFormVisible" :before-close="beforeClose">
      <el-form :model="form">
        <el-form-item label="链接地址：" :label-width="formLabelWidth">
          <el-input v-model="form.url" minlength="2" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="排序比重：" :label-width="formLabelWidth">
          <el-input v-model="form.sort" placeholder="请输入排序比重" />
        </el-form-item>
        <el-form-item label="选择店铺：" :label-width="formLabelWidth" v-if="this.$store.getters.role.store_id == 0">
          <el-select v-model="form.store_id" placeholder="请选择店铺" style="width: 100%;">
            <el-option
              v-for="item in shops"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="轮播位置：" :label-width="formLabelWidth">
              <el-select v-model="form.type" placeholder="请选择广告位置">
                <el-option label="首页" value="1" />
                <el-option label="个人中心" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="轮播状态：" :label-width="formLabelWidth">
              <el-switch
                v-model="form.status"
                :active-text="form.status==1?'启用':'禁用'"
                active-value="1"
                inactive-value="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="轮播封面：" :label-width="formLabelWidth">
          <image-handle
            type="banner"
            :files="files"
            :sum="1"
            @returnFile="getFile"
          />

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="canel">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { handle } from '@/api/banner'
import { getStore } from '@/api/role'
import ImageHandle from '@/components/Image'
export default {
  name: 'MenuHandle',
  components: { ImageHandle },
  data() {
    return {
      form: {
        id: 'new',
        store_id: 0,
        url: '',
        cover: '',
        type: '1',
        status: '1',
        sort: ''
      },
      shops: [],
      formLabelWidth: '120px',
      dialogFormVisible: false,
      files: [],
      pageTitle: '添加轮播'
    }
  },

  mounted() {
    getStore({}).then(res => {
      this.shops = res.data
    })
  },

  methods: {
    beforeClose() {
      this.canel()
    },

    getFile(file) {
      if (file.length === 0) {
        this.form.cover = ''
        return
      }
      this.form.cover = file[0].origin
    },

    add() {
      this.form.id = 'new'
      this.pageTitle = '添加轮播'
      this.dialogFormVisible = true
    },

    edit(item) {
      this.form.id = item.id
      this.form.store_id = item.store_id
      this.form.url = item.url
      this.form.cover = item.cover
      this.form.sort = item.sort
      this.form.type = item.type.toString()
      this.form.status = item.status.toString()
      this.pageTitle = '编辑轮播'
      this.dialogFormVisible = true
      const arr = { 'origin': item.origin, 'url': item.cover }
      const that = this
      setTimeout(function() {
        that.files.push(arr)
      }, 1)
    },

    canel() {
      this.dialogFormVisible = false
      this.form.store_id = 0
      this.form.id = 'new'
      this.form.url = ''
      this.form.cover = ''
      this.form.status = '1'
      this.form.type = '1'
      this.form.sort = ''
      this.files = new Array()
    },

    sure() {
      handle(this.form).then(res => {
        Message({
          message: res.data.info,
          type: 'success',
          duration: 2000
        })
        this.$emit('afterSave')
        this.canel()
      })
    }
  }
}
</script>
